package cn.gjxblog.demo;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * 作用: 测试Shiro认证,使用系统数据源
 * 作者:gjx
 * 版本:V1.0
 *
 */
public class TestShiro {
    public static void main(String[] args) {
        //创建sessionFactory
        IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        //创建安全管理器
        SecurityManager securityManager = factory.getInstance();
        //将安全管理器设置到工具类中
        SecurityUtils.setSecurityManager(securityManager);
        //获取主体
        Subject subject = SecurityUtils.getSubject();
        //创建用户密码令牌
        UsernamePasswordToken token = new UsernamePasswordToken("xiaohei1", "123456");

        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }

        System.out.println(subject.isAuthenticated());

    }
}
